<?php

declare(strict_types=1);

namespace app\model\system;

use app\service\system\RoleService;
use owns\auth\UserAuth;
use owns\interface\OwnsModelInteface;

class MenuModel extends OwnsModelInteface
{
    protected $name = 'system_menu';

    protected $field = [];

    /**
     * 查询条件
     * @param object $query
     * @param array $param
     */
    public function scopeCustom(object $query, array $param)
    {
        // 菜单名称
        if ($title = $param['title'] ?? '') {
            $query->whereLike('title', '%' . trim($title) . '%');
        }
        // 角色
        $auth = UserAuth::instance();
        if(!$auth->root()) {
            $query->whereIn('id', RoleService::roleMenuId($auth->role()));
        }
    }

    public function apiList()
    {
        return $this->hasMany(MenuApiModel::class, 'menu_id', 'id');
    }
}
